package com.newcoder.community.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @author edison
 * @version 2023
 * @date 2024/1/14 17:52
 */

@Controller
@RequestMapping("/testspringmvc")
public class TestSpringMVCController {

    @RequestMapping("/http") //浏览器访问 http://localhost:8080/community/testspringmvc/http
    public void http(HttpServletRequest request, HttpServletResponse response) {
        // 获取请求数据
        System.out.println(request.getMethod()); // 获取请求方式
        System.out.println(request.getServletPath()); // 获取请求路径
        Enumeration<String> enumeration = request.getHeaderNames();// 获取请求行，key-value形式
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            System.out.println(name + ":" + value);
        }
        System.out.println(request.getParameter("code")); // 获取请求参数

        // 返回响应数据，封装复杂方法
        response.setContentType("text/html;charset=utf-8"); // 返回一个html页面
        try (
                PrintWriter writer = response.getWriter();
        ) {
            writer.write("<h1>牛客网</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }

    }

    // GET请求
    // /students?current=1&limit=20
    // 查询所有学生
    @RequestMapping(value = "/students", method = RequestMethod.GET)
    //浏览器访问 http://localhost:8080/community/testspringmvc/students?current=1&limit=20
    @ResponseBody
    //name:请求参数名，required:是否必须，默认为true，defaultValue:默认值
    public String getStudents(@RequestParam(name = "current", required = false, defaultValue = "1") int current,
                              @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        System.out.println(current);
        System.out.println(limit);
        return "some students";

    }

    // GET请求2
    // /student/123
    // 查询某个学生
    @RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
    //浏览器访问 http://localhost:8080/community/testspringmvc/student/123
    @ResponseBody
    public String getStudent(@PathVariable("id") int id) {
        System.out.println("id" + id);
        return "a student";
    }

    // POST请求
    @RequestMapping(value = "/student", method = RequestMethod.POST)
    //浏览器访问 http://localhost:8080/community/testspringmvc/student
    @ResponseBody
    public String saveStudent(String name, int age) {
        System.out.println("name:" + name);
        System.out.println("age:" + age);
        return "save success";
    }

    // 响应HTML数据方法一
    @RequestMapping(value = "/teacher", method = RequestMethod.GET)
    //浏览器访问 http://localhost:8080/community/testspringmvc/teacher
    public ModelAndView getTeacher() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", "张三");
        mav.addObject("age", 30);
        mav.setViewName("/demo/view"); // view.html
        return mav;
    }

    // 响应HTML数据方法二
    @RequestMapping(value = "/school", method = RequestMethod.GET)
    //浏览器访问 http://localhost:8080/community/testspringmvc/school
    public String getSchool(Model model) {
        model.addAttribute("name", "北京大学");
        model.addAttribute("age", 80);
        return "/demo/view"; // view.html
    }

    // 响应JSON数据（异步请求）
    // Java对象 -> JSON字符串 -> JS对象
    // 比如在网页注册一个用户，输入名称，显示该昵称是否被占用，即当前网页不刷新，自动访问数据库
    // 浏览器访问 http://localhost:8080/community/testspringmvc/emp
    @RequestMapping(value = "/emp", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEmp() {
        Map<String, Object> emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", 23);
        emp.put("salary", 8000.00);
        return emp;
    }


    // 浏览器访问 http://localhost:8080/community/testspringmvc/emps
    @RequestMapping(value = "/emps", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> getEmps() {
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> emp = new HashMap<>();
        emp.put("name", "平什么");
        emp.put("age", "100");
        emp.put("salary", "100000");
        list.add(emp);

        emp = new HashMap<>();
        emp.put("name", "张三");
        emp.put("age", "213");
        emp.put("salary", "4300");
        list.add(emp);

        emp = new HashMap<>();
        emp.put("name", "阿斯蒂芬");
        emp.put("age", "345");
        emp.put("salary", "970");
        list.add(emp);

        return list;
    }

}
